/**
 * Created by AllanXu on 7/25/2016.
 */
define(function (require) {
    var Vue = require('vue'),
        Config = require('config'),
        _ = require('ajax'),
        utilities = require('utilities'),
        userId = sessionStorage.id;

    var addCode = sessionStorage.getItem("addCode") ? sessionStorage.getItem("addCode") : "33";

    var id = utilities.getParameterByName('id');

    if (typeof id !== 'undefined' && id !== '') {
        sessionStorage.setItem('id', id);
        userId = sessionStorage.id;
    } else {
        if (typeof sessionStorage.id === 'undefined') {
            location.href = 'phone_bind.html?openId=' + utilities.getParameterByName('openId');
        }
    }

    var type = utilities.getParameterByName('type'), params = {};
    if (type == 1) {
        params = {
            'xgmate.type': 3
        };
    } else if (type == 2) {
        params = {
            'xgmate.type': 2
        };
    } else if (type == 3) {
        params = {
            'xgmate.type': 1
        };
    } else if (type == 4) {
        params = {
            'xgmember.sex': 403
        };
    } else if (type == 5) {
        params = {
            'xgmember.sex': 402
        };
    }

    // 认证
    var AuthDialog = require('auth_dialog');
    var authDialog = new AuthDialog();
    // 禁用申诉
    var Forbidden = require('forbidden');
    var forbidden = new Forbidden();

    var app = new Vue({
        el: '#friend_index',
        data: {
            pageCount: '',
            pageno: 1,
            renderObj: {
                listArr: []
            },
            tabsName: [],
            typeId: '',
            curNum: -1,
            bottomTxt: '',
            // 筛选
            filter: false,
            filterArr: '',
            ageArr: [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45],
            sex: '',
            ageMin: '',
            ageMax: '',
            marriage: '',
            // 接口参数使用
            selectedAgeMin: '',
            selectedAgeMax: '',
            
            // 模糊查找
            mateName: '',

            educations: '',
            income: '',
            // 地区
            cityArr: [],
            countyArr: '',
            // 挑选的市县id
            selectedCityId: '',
            selectedCountyId: '',
            region: '',

            // 打招呼
            msgPageno: 1,
            msgDialog: false,
            msgPageCount: '',
            msgList: [],
            selectMsgIndex: -1,
            selectMsg: '',
            mateId: '',
            // 发信息
            showSendMsg: false,
            msgContent: '',
            picServer: Config.picServer,
            defaultAdd: '江西省'
        },
        watch: {
            // 选择城市
            selectedCityId: function () {
                //console.log(111);
                app.region = app.selectedCityId;
                app.selectCounty();
            },
            // 选择县
            selectedCountyId: function () {
                app.region = app.selectedCountyId;
            },
            // 选择最小年龄
            ageMin: function () {
                //console.log(1);
                if(app.ageMax!==''){
                    if (app.ageMax < app.ageMin) {
                        app.selectedAgeMax = app.ageMin;
                        app.selectedAgeMin = app.ageMax;
                        //console.log("最小:",app.selectedAgeMin,"最大:", app.selectedAgeMax);
                    }
                }else{
                    app.selectedAgeMin = app.ageMin;
                }
                //console.log(app.selectedAgeMin, app.selectedAgeMax,1)
            },
            // 选择最大年龄
            ageMax: function () {

                if(app.ageMin!==''){
                    if (app.ageMax < app.ageMin) {
                        app.selectedAgeMax = app.ageMin;
                        app.selectedAgeMin = app.ageMax;
                        //console.log("最小:",app.selectedAgeMin,"最大:", app.selectedAgeMax);
                    }else{
                        app.selectedAgeMax = app.ageMax;
                    }
                }else{
                    app.selectedAgeMax = app.ageMax;
                }
                
                //console.log(app.selectedAgeMin, app.selectedAgeMax,2)
            }
        },
        methods: {
            init: function () {
                var addName = sessionStorage.getItem('addName');
                app.defaultAdd = addName || app.defaultAdd;
                //app.cityArr
                if(app.region!=''){
                	
                    for (var key in app.cityArr) {
                        if(app.cityArr[key].id==app.region)
                            app.region=app.cityArr[key].code;
                    }
                    for (var key in app.countyArr) {
                        if(app.countyArr[key].id==app.region)
                            app.region=app.countyArr[key].code;
                    }
                }
                var in_sex=utilities.getParameterByName('sex');
                app.sex=app.sex||in_sex;
                var in_marriage=utilities.getParameterByName('marriage');
                app.marriage=app.marriage||in_marriage;
                var in_selectedAgeMin=utilities.getParameterByName('selectedAgeMin');
                app.selectedAgeMin=app.selectedAgeMin||in_selectedAgeMin;
                var in_selectedAgeMax=utilities.getParameterByName('selectedAgeMax');
                app.selectedAgeMax=app.selectedAgeMax||in_selectedAgeMax;
                var in_educations=utilities.getParameterByName('educations');
                app.educations=app.educations||in_educations;
                var in_income=utilities.getParameterByName('income');
                app.income=app.income||in_income;
                var in_region=utilities.getParameterByName('region');
                app.region=app.region||in_region;
                var in_mate = utilities.getParameterByName('mateName');
                app.mateName=app.mateName||in_mate;
                var _params = {
                    'id': userId,
                    'pageno': app.pageno,
                    'pagesize': 10,
                    'xgmember.marry': app.marriage,
                    'xgmember.money': app.income,
                    'xgmember.sex': app.sex,
                    'xgmate.ageBegin': app.selectedAgeMin,
                    'xgmate.ageEnd': app.selectedAgeMax,
                    'xgmate.code': app.region,
                    'xgmember.education': app.educations,
                    'code': app.region||addCode,
                    'xgmate.name': app.mateName
                };
                for (var key in params) {
                    _params[key] = params[key];
                }
                console.log(_params)
                _(Config.SERVICE.MATES.LIST, _params, function (data) {
                	console.log(data)
                        if (app.pageno == 1) {
                            app.renderObj.listArr = data.pageitems;
                            app.pageCount = data.pagecount;
                        } else {
                            app.renderObj.listArr = app.renderObj.listArr.concat(data.pageitems);
                        }
                    }
                );

                app.switchFilter();
            },
            goDetail: function (id) {
                location.href = 'friend_detail.html?id=' + id;
            },
            // 筛选
            switchFilter: function () {
                //app.filter = !app.filter;

                app.selectedAgeMin = '';
                app.selectedAgeMax = '';

                // 6婚姻状态
                _(Config.DICTIONARY + '?parentId=340,8,418,2,6', {}, function (data) {
                    //console.log("筛选性别等:", data);
                    app.filterArr = data.obj;
                });
                // 居住地
                _(Config.SERVICE.MATES.ADDR + '?parentId=2', {}, function (data) {
                    //console.log("地区:", data);
                    app.cityArr = data.obj[2];
                });
            },
            selectCounty: function () {
                // 居住地
                _(Config.SERVICE.MATES.ADDR + '?parentId=' + app.selectedCityId, {}, function (data) {
                    //console.log(data);
                    app.countyArr = data.obj[app.selectedCityId];
                });
            },
            confirmFilter: function () {
                //app.filter = !app.filter;
                app.init();
            },
            // 打招呼
            switchSayhi: function () {
                app.msgDialog = !app.msgDialog;
            },
            sayhi: function (mateId) {
                // 是否认证/禁用
                var userInfo = JSON.parse(decodeURIComponent(atob(sessionStorage.userInfo)));
                if (userInfo.member.sfgb == 1) {	// 认证
                    if (confirm("您已脱单，如需继续使用，请点击恢复单身。")) {
                        location.href = 'uc_index.html';
                    }
                    return;
                }
                /*if (userInfo.member.sfrz == 0) {    // 认证
                    authDialog.show();
                } else if (userInfo.member.sfjy == 1) {  // 禁用
                    forbidden.show();
                } else {
                    console.log("已认证、没有禁用");

                    _(Config.SERVICE.MATES.VERIFY + '?id=' + userId, {}, function (data) {
                        console.log(data);
                        if (data.errmsg.indexOf("择偶") > 0) {
                            // 择偶条件没有完善跳转到择偶条件
                            if (confirm("为更好地为您提供服务，请在“个人编辑”中完善择偶条件。")) {
                                location.href = 'uc_mate_condition.html';
                            }
                        } else if (data.errmsg.indexOf("基本信息") > 0) {
                            if (confirm("为更好地为您提供服务，请在“个人编辑”中完善个人资料。")) {
                                location.href = 'uc_profile.html';
                            }
                        }else if(data.errmsg.indexOf("形象照") > 0){
                            if(confirm("为更好地为您提供服务，请在“个人编辑”中上传形象照。")){
                                location.href = 'uc_basicInfo.html';
                            }
                        } else {*/
                            app.msgDialog = true;
                            app.mateId = mateId;
                            _(Config.SERVICE.MATES.GREETEDLIST + '?pageno=' + app.msgPageno + '&pagesize=5', {}, function (data) {
                                app.msgPageCount = data.pagecount;
                                app.msgList = data.pageitems;
                            });
                       /* }
                    });
                }*/

            },
            exchangeMsg: function () {
                if (app.msgPageno >= app.msgPageCount) {
                    app.msgPageno = 1;
                } else {
                    app.msgPageno++;
                }
                app.sayhi(app.mateId);
            },
            selectMsgFn: function (index, content) {
                //console.log(index)
                app.selectMsgIndex = index;
                app.selectMsg = content;
            },
            submitMsg: function () {
                if (app.mateId == userId) {
                    alert("不能发送给自己~");
                } else {
                    _(Config.SERVICE.MATES.SAYHI + '?conMsg.memberA.id=' + userId + '&conMsg.memberB.id=' + app.mateId + '&conMsg.msg=' + app.selectMsg + '&conMsg.type=1', {}, function (data) {
                        if (data.code == 200) {
                            app.selectMsgIndex = -1;
                            app.selectMsg = '';
                            app.msgDialog = false;
                            alert("发送成功~");
                        }
                    });
                }
            },
            // 发信息
            sendMsgDialog: function (mateId2) {
                // 是否认证/禁用
                var userInfo = JSON.parse(decodeURIComponent(atob(sessionStorage.userInfo)));
                if (userInfo.member.sfgb == 1) {	// 认证
                    if (confirm("您已脱单，如需继续使用，请点击恢复单身。")) {
                        location.href = 'uc_index.html';
                    }
                    return;
                }
                /*var userInfo = JSON.parse(decodeURIComponent(atob(sessionStorage.userInfo)));
                if (userInfo.member.sfrz == 0) {    // 认证
                    authDialog.show();
                } else if (userInfo.member.sfjy == 1) {  // 禁用
                    forbidden.show();
                } else {
                    console.log("已认证、没有禁用");

                   _(Config.SERVICE.MATES.VERIFY + '?id=' + userId, {}, function (data) {
                        console.log(data);
                        if (data.errmsg.indexOf("择偶") > 0) {
                            // 择偶条件没有完善跳转到择偶条件
                            if (confirm("为更好地为您提供服务，请在“个人编辑”中完善择偶条件。")) {
                                location.href = 'uc_mate_condition.html';
                            }
                        } else if (data.errmsg.indexOf("基本信息") > 0) {
                            if (confirm("为更好地为您提供服务，请在“个人编辑”中完善个人资料。")) {
                                location.href = 'uc_profile.html';
                            }
                        }else if(data.errmsg.indexOf("形象照") > 0){
                            if(confirm("为更好地为您提供服务，请在“个人编辑”中上传形象照。")){
                                location.href = 'uc_basicInfo.html';
                            }
                        } else {*/
                            app.mateId = mateId2;
                            app.showSendMsg = !app.showSendMsg;
                        /*}
                    });
                }*/
            },
            sendMsg: function (msgContent) {
                if (app.mateId == userId) {
                    alert("不能发送给自己~")
                } else {
                    _(Config.SERVICE.MATES.SAYHI + '?conMsg.memberA.id=' + userId + '&conMsg.memberB.id=' + app.mateId + '&conMsg.msg=' + msgContent + '&conMsg.type=2', {}, function (data) {
                        console.log(data);
                        if (data.code == 200) {
                            app.msgContent = '';
                            app.showSendMsg = false;
                            alert("发送成功");
                        }
                    });
                }
            }
        }
    });
    app.init();

    window.onscroll = function () {
        var scrolltop = document.body.scrollTop | document.documentElement.scrollTop;
        var clientHeight = document.documentElement.clientHeight;
        if (scrolltop + clientHeight == document.body.clientHeight) {
            if (app.pageno <= app.pageCount) {
                app.pageno++;
                app.init();
            } else {
                app.bottomTxt = '到底儿了~';
            }
        }
    }
});